<?php
require_once("Service.php");
require_once("Database.php");

class Home extends Service {
	
	var $conn;
	
	/** The constructor of this object. */
	function __construct() {
		
		parent::__construct();
		$this->conn = new Database();
	}
	
	/**
	 * Returns the home page post.
	 * @param $sessionID The session id of the user calling this function.
	 * @return A object containing the home page post.
	 */
	function getHomePage($sessionID) {
		
		$this->validateSession($sessionID);
		
		$result = $this->conn->execute("
			SELECT
				b.firstName,
				b.lastName,
				a.header,
				a.body,
				a.date
			FROM Posts a
			LEFT OUTER JOIN Users b
				ON a.userID = b.userID
			WHERE a.postID = 1
		");
		
		return mysql_fetch_assoc($result);
	}
	
	/**
	 * Saves the home page.
	 * @param $sessionID The session id of the user calling this function.
	 * @param $userID The id of the user doing the update.
	 * @param $header The header of the post.
	 * @param $body The body of the post.
	 */
	function saveHomePage($sessionID, $userID, $header, $body) {
		
		$this->validateSession($sessionID);
		
		$result = $this->conn->execute("
			UPDATE Posts
			SET
				userID = " . $userID . ",
				header = '" . $header . "',
				body = '" . $body . "',
				date = '" . date("Y-m-d H:i:s") . "'
			WHERE postID = 1
		");
	}
}
?>